from django.http import *
from django.shortcuts import render
from django.views import View
from home.models import *


class IndexView(View):
    def get(self, request):
        # 1、获取所有分类信息数据
        categories = ArticleCategory.objects.all()

        # 2、接收用户点击的分类id
        cat_id = request.GET.get('cat_id', 1)
        # 3、根据分类id进行分类的查询
        try:
            category = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
             return HttpResponseBadRequest('此分类信息不存在')
        # 4、获取分页参数
        page_index = request.GET.get('page_index', 1)  # 页码
        page_size = request.GET.get('page_size', 3)  # 页容量
        # 5、根据分类信息查询该分类下的所有文章数据
        articles = Article.objects.filter(category=category)
        # 6、创建分页器
        from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger  # 导入django分页插件
        pages = Paginator(articles, page_size)  # 对查询到的数据对象articles进行分页，设置超过指定页容量就分页
        #
        try:
            list = pages.page(page_index)  # 获取当前页面的记录
        except PageNotAnInteger:
            list = pages.page(1)  # 如果用户输入的页面不是整数时，显示第1页的内容
        except EmptyPage:
            list = pages.page(pages.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
        # 4、组织数据传递给模板
        context = {
            'categories': categories,
            'category': category,
            'articles': list,
            'cat_id':cat_id
        }
        print(list)
        return render(request, 'index.html',context=context)

class DetailView(View):
    def get(self, request):
        art_id = request.GET.get('art_id')
        try:
            art=Article.objects.get(id=art_id)
        except Article.DoesNotExist:
            return render(request,'404.html')
        art.total_views += 1
        art.save()
        hot_tags = Article.objects.values('tags').order_by('-total_views').distinct()[:9]
        new_arts = Article.objects.order_by('-create_time')[:2]
        context = {
            'article':art,
            'hot_tags':hot_tags,
            'new_arts':new_arts

        }
        return render(request, 'details.html',context=context)



